﻿using System;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PublicDomain.Data.Csv.Metadata
{
    [DataContract(Namespace = "www.publicdomain.net\\data\\csv\\metadata\\2013-11-26\\", Name = "CsvSchemata")]
    public class CsvSchemata
    {
        [DataMember(Name = "CsvDataFormats")]
        public Collection<CsvDataFormat> CsvDataFormats { get; set; }

        [DataMember(Name = "CsvFilesFormats")]
        public Collection<CsvFileFormat> CsvFilesFormats { get; set; }

        public CsvDataFormat GetCsvFileDataFormat(string fileName)
        {
            var result = this.GetCsvDataFormat(this.GetCsvFileDataFormatName(fileName));

            return result;
        }

        public string GetCsvFileDataFormatName(string fileName)
        {
            if (this.CsvFilesFormats != null)
            {
                foreach (var csvFilesFormat in this.CsvFilesFormats)
                {
                    if (!String.IsNullOrWhiteSpace(csvFilesFormat.FileName) && csvFilesFormat.FileName.Equals(fileName, StringComparison.OrdinalIgnoreCase))
                    {
                        return csvFilesFormat.CsvDataFormatName;
                    }
                }
            }

            return String.Empty;
        }

        public CsvDataFormat GetCsvDataFormat(string dataFormatName)
        {
            foreach (var csvDataFormat in this.CsvDataFormats)
                {
                    if (!String.IsNullOrWhiteSpace(csvDataFormat.Name) && csvDataFormat.Name.Equals(dataFormatName, StringComparison.OrdinalIgnoreCase))
                    {
                        return csvDataFormat;
                    }
                }

            return null;
        }
    }
}
